const cars = {
	state:{
		carsList:[]
	},
	mutations:{
		setCarList(state,params){
			let {item,num} = params;
			console.log(params,state.carsList);
			let carItem = {
				goodsid:item._id || item.goodsid,
				numvalue:num,
				name:item.name,
				price:item.price,
				before_price:item.before_price,
				thumb:item.thumb,
				skuArr: item.skuArr ? item.skuArr.slice().sort() : null,
				add:item.add || false
			}
			let idx = state.carsList.findIndex(car =>{
				if (car.skuArr && carItem.skuArr) {
				        return (
				          car.goodsid === carItem.goodsid &&
				          JSON.stringify(car.skuArr) === JSON.stringify(carItem.skuArr)
				        );
				      } else {
				        return car.goodsid === carItem.goodsid;
				      }
				    });
			// console.log(idx,caritem.skuArr);
			
			if(idx<0){
				state.carsList.unshift(carItem)
			}else{
				if(carItem.add){
					state.carsList[idx].numvalue += carItem.numvalue;
				}else{
					state.carsList[idx].numvalue = carItem.numvalue;
				}
				
				// 如果数量为0，就删除这个商品
				if(state.carsList[idx].numvalue<=0) {state.carsList.splice(idx,1);}
			}
			
			
			
			
		},
		CLEAR_CAR(state){
			state.carsList = [];
		}
	}
}

export default cars